﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NFine.Code;
using NFine.Domain._04_IRepository.Biz;
using NFine.Repository.Biz;
using NFine.Domain._03_Entity.Biz;
using NFine.Domain.IRepository.SystemManage;
using NFine.Repository.SystemManage;

namespace NFine.Application.Biz
{
    /// <summary>
    /// 中转中心品鉴积分
    /// </summary>
    public class ZhongZhuanPinJianApp
    {
        private IZhongZhuanPinJianRepository zhongZhuanPinJianRepos = new ZhongZhuanPinJianRepository();
        private IUserRepository userRepository = new UserRepository();


        public List<ZhongZhuanPinJianEntity> GetList(Pagination pagination, DateTime dtstart, DateTime dtend,int type)
        {
            var expression = ExtLinq.True<ZhongZhuanPinJianEntity>();

            expression = expression.And(a => a.F_CreateTime >= dtstart && a.F_CreateTime < dtend);
            if (type == 1)
            {
                expression = expression.And(a => a.F_JLIntegral > 0);
            }
            else if (type == 2)
            {
                expression = expression.And(a => a.F_JLIntegral < 0);
            }
            else if (type == 3)
            {
                expression = expression.And(a => a.F_Type == 0);
            }
            else if (type == 4)
            {
                expression = expression.And(a => a.F_Type == 1);
            }
            List<ZhongZhuanPinJianEntity> list = new List<ZhongZhuanPinJianEntity>();
            var user = OperatorProvider.Provider.GetCurrent();
            if (user.IsSys)
            {
                list = zhongZhuanPinJianRepos.FindList(expression, pagination);
                string roleid = Configs.GetValue("TransferCenterRoleId");
                var users = userRepository.FindList(t => t.F_RoleId == roleid);
                foreach (var zhzh in list)
                {
                    var u = users.Find(t => t.F_Id == zhzh.F_UserID);
                    if (u != null)
                    {
                        zhzh.userName = u.F_RealName;
                    }
                }
            }
            else
            {
                expression = expression.And(a => a.F_UserID == user.UserId);
                list = zhongZhuanPinJianRepos.FindList(expression, pagination);
            }
            return list;
        }

        public bool SubmitForm(string uid, decimal pjint, decimal jlint)
        {
            ZhongZhuanPinJianEntity zhzh = new ZhongZhuanPinJianEntity();
            zhzh.F_Id = Common.CreateNo();
            zhzh.F_CreateTime = DateTime.Now;
            zhzh.F_Integral = pjint - jlint;
            zhzh.F_JLIntegral = jlint * -1;
            zhzh.F_UserID = uid;
            zhzh.F_Type = 0;
            return zhongZhuanPinJianRepos.Insert(zhzh) > 0;
        }
        /// <summary>
        /// 换购记录提交
        /// </summary>
        /// <param name="zzid">中转中心的ID</param>
        /// <param name="pjint">积分余额</param>
        /// <param name="jlint">积分换购金额</param>
        /// <returns></returns>
        public bool HGSubmitForm(string zzid, decimal pjint, decimal jlint)
        {
            var user= OperatorProvider.Provider.GetCurrent();
            var zzUser = userRepository.FindEntity(zzid);
            ZhongZhuanPinJianEntity zhzh = new ZhongZhuanPinJianEntity();
            zhzh.F_Id = Common.CreateNo();
            zhzh.F_CreateTime = DateTime.Now;
            zhzh.F_Integral = pjint - jlint;
            zhzh.F_JLIntegral = jlint * -1;
            zhzh.F_UserID = user.UserId ;
            zhzh.F_Type = 1;
            zhzh.F_Info = "向"+zzUser.F_RealName+"【"+zzUser.F_Account+"】支付的换购积分！";

            ZhongZhuanPinJianEntity zhzhzz = new ZhongZhuanPinJianEntity();
            zhzhzz.F_Id = zhzh.F_Id.Substring(0,19) + "-";// Common.CreateNo();
            zhzhzz.F_CreateTime = DateTime.Now;
            zhzhzz.F_Integral = zhongZhuanPinJianRepos.GetZhZhJF(zzid) + jlint;
            zhzhzz.F_JLIntegral = jlint ;
            zhzhzz.F_UserID = zzid;
            zhzhzz.F_Type = 0;
            zhzhzz.F_Info = user.UserName + "【" + user.UserCode + "】支付的换购积分！";


            return zhongZhuanPinJianRepos.InsertHG(zhzh, zhzhzz);
        }

        public decimal GetZhZhJF(string userid)
        {
            return zhongZhuanPinJianRepos.GetZhZhJF(userid);
        }
    }
}
